#include "UserInput.h"

#define KEYSTROKE_INTERVAL .2f

UserInput::UserInput(void)
{
	timeActionMade = 0.0f;
	isKeyPressed = false;
	isStaticDrop = true;
	isDynamicDrop = false;
}

UserInput::~UserInput(void)
{
}

void UserInput::initFrameInput(float currentTime)
{
	if(currentTime - timeActionMade > KEYSTROKE_INTERVAL)
		isKeyPressed = false;
}

void UserInput::addItemsToScene(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
	if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left  ) && !isKeyPressed ) executeLeftMouse(worldObjects, currentTime, window);
	if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Right ) && !isKeyPressed ) executeRightMouse(worldObjects, currentTime, window);
	if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Middle) && !isKeyPressed ) executeMiddleMouse(worldObjects, currentTime, window);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::L          ) && !isKeyPressed ) executeLKey(worldObjects, currentTime, window);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Delete     )                  ) executeDeleteKey(worldObjects);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape     )                  ) executeEscapeKey(window);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::S          ) && !isKeyPressed ) executeSKey(currentTime);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::D          ) && !isKeyPressed ) executeDKey(currentTime);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Tilde      ) && !isKeyPressed ) executeTildeKey(worldObjects, currentTime, window);
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::T          )                  ) executeTKey(worldObjects);
}

////////////////////PRIVATE METHODS///////////////////////////

void UserInput::executeLeftMouse(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
	sf::Vector2i position = sf::Mouse::getPosition(*window);
	if(isStaticDrop)
		worldObjects->addBox(b2Vec2(32, 32), b2Vec2(position.x, -position.y), STATIC_BODY, 1.0f, 5.3f, sf::Color::Color(120, 230, 200, 140));
	else
		worldObjects->addBox(b2Vec2(32, 32), b2Vec2(position.x, -position.y), DYNAMIC_BODY, 1.0f, 5.3f, sf::Color::Color(250, 230, 200, 140));
	timeActionMade = currentTime;
	isKeyPressed = true;
}

void UserInput::executeRightMouse(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
	sf::Vector2i position = sf::Mouse::getPosition(*window);
	if(isStaticDrop)
		worldObjects->addBox(b2Vec2(16, 16), b2Vec2(position.x, -position.y), STATIC_BODY, 1.0f, 5.3f, sf::Color::Color(0, 0, 200, 140));
	else
		worldObjects->addBox(b2Vec2(16, 16), b2Vec2(position.x, -position.y), DYNAMIC_BODY, 1.0f, 5.3f, sf::Color::Color(100, 0, 200, 140));
	timeActionMade = currentTime;
	isKeyPressed = true;
}

void UserInput::executeMiddleMouse(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
	sf::Vector2i position = sf::Mouse::getPosition(*window);
	worldObjects->addCircle(16.0f, b2Vec2(position.x, -position.y), DYNAMIC_BODY, 2.0f, 3.0f, sf::Color::Color(100, 100, 100, 140));
	timeActionMade = currentTime;
	isKeyPressed = true;
}


void UserInput::executeLKey(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
	sf::Vector2i position = sf::Mouse::getPosition(*window);
	if(isStaticDrop)
		worldObjects->addBox(b2Vec2(64, 64), b2Vec2(position.x, -position.y), STATIC_BODY, 100.0f, 5.3f, sf::Color::Color(0, 200, 135, 140));
	else
		worldObjects->addBox(b2Vec2(64, 64), b2Vec2(position.x, -position.y), DYNAMIC_BODY, 100.0f, 5.3f, sf::Color::Color(135, 200, 0, 140));
	timeActionMade = currentTime;
	isKeyPressed = true;
}

void UserInput::executeTKey(ObjectManager* worldObjects)
{
	worldObjects->getActiveRectList().at(worldObjects->getActiveRectList().size() - 1)->rectBody->ApplyForce(b2Vec2(13300.0f, 0), b2Vec2(0, 0));
}

void UserInput::executeDKey(float currentTime)
{
	if(!isDynamicDrop)
	{
		isStaticDrop = false;
		isDynamicDrop = true;
	}
	timeActionMade = currentTime;
	isKeyPressed = true;
}

void UserInput::executeSKey(float currentTime)
{
	if(!isStaticDrop)
	{
		isStaticDrop = true;
		isDynamicDrop = false;
	}
	timeActionMade = currentTime;
	isKeyPressed = true;
}

void UserInput::executeDeleteKey(ObjectManager* worldObjects)
{
	worldObjects->clearObjects();
}

void UserInput::executeEscapeKey(sf::RenderWindow* window)
{
	window->close();
}

void UserInput::executeTildeKey(ObjectManager* worldObjects, float currentTime, sf::RenderWindow* window)
{
}
